# -*- coding: utf-8 -*-
import pymongo
from datetime import date,timedelta
client = pymongo.MongoClient('127.0.0.1')
db = client.city4
import re
from MySQLdb import *
conn = connect("127.0.0.1","root","123456789", "shantianjun",charset="utf8mb4",cursorclass=cursors.DictCursor)
cursor = conn.cursor()
cursor.execute("select * from girl")
girl = cursor.fetchallDict()
girls = {}
for x in girl:
    girls[x["url"]] =x["id"]
stores = {}
cursor.execute("select * from store")
store = cursor.fetchallDict()
for x in store:
    stores[x["url"]] = x["id"]
data = {}
urlpool = set()
turnList = set()
for y in db.stores.find():
        if y["storesUrl"] in urlpool:
            print "continue"
            continue
        i = 0
        for x in y["attendance"]:
            now = date.today() + timedelta(days=i)
            print i
            for yy in x:
                # print y["storesUrl"]
                if not girls.get(yy.replace("https://www.cityheaven.net","")):
                    print yy
                    print y["storesUrl"]
                    continue
                url = y["storesUrl"].replace("https://www.cityheaven.net","")
                url2 = yy.replace("https://www.cityheaven.net","")
                turnList.add((stores[url],girls.get(url2),now))
            i += 1
        urlpool.add(y["storesUrl"])
sql = "insert into turn (sid,gid,date) values (%s,%s,%s)"
cursor.executemany(sql,turnList)
cursor.close()
conn.commit()
# insert info cate (name) select select distinct(cate) from stores where cate <> ""
